/******************************************************************
 Copyright (C) 2019 - All Rights Reserved by
 文 件 名 : shellwidget.cpp --- ShellWidget
 作 者    : Niyh(lynnhua)
 编写日期 : 2019
 说 明    :
 历史纪录 :
 <作者>    <日期>        <版本>        <内容>
  Niyh	   2019    	1.0.0 1     文件创建
*******************************************************************/
#include "shellwidget.h"

#include <QApplication>
#include <QClipboard>
#include <QKeyEvent>
#include <QDebug>
#include <QTextCursor>
#include <QTextDocument>
#include <QTextDocumentFragment>

ShellWidget::ShellWidget(QWidget *parent) : QTextEdit(parent)
{

    this->insertedChars = 0;
    this->cursorPosition = 0;
    this->commandHistoryPosition = 0;
    this->cursor = this->textCursor();
    this->setCursorWidth(3);
    this->setTextCursor(cursor);
    this->setContextMenuPolicy(Qt::CustomContextMenu);
}

ShellWidget::~ShellWidget()
{

}

void ShellWidget::keyPressEvent(QKeyEvent *e)
{
    if (e->modifiers() == Qt::ControlModifier)
    {
        if (e->key() == Qt::Key_C)
        {
            emit signalCommand("InterruptProcess");
        }
        else if (e->key() == Qt::Key_Left)
        {
            if (this->cursorPosition < this->insertedChars)
            {
                int pos = this->cursor.position();
                this->cursor.movePosition(QTextCursor::PreviousWord);
                this->setTextCursor(this->cursor);
                this->cursorPosition += pos-this->cursor.position();
            }
        }
        else if (e->key() == Qt::Key_Right)
        {
            if (this->cursorPosition > 0)
            {
                int pos = this->cursor.position();
                this->cursor.movePosition(QTextCursor::NextWord);
                this->setTextCursor(this->cursor);
                this->cursorPosition-=this->cursor.position()-pos;
            }
        }
        else if (e->key() == Qt::Key_Backspace)
        {
            //usun poprzedzajace slowo
        }
        else if (e->key() == Qt::Key_Delete)
        {
            //usun nastepne slowo
        }

        return;
    }
    else if ((e->modifiers() & Qt::SHIFT) && (e->modifiers() & Qt::CTRL))
    {
        if (e->key() == Qt::Key_V)
        {
            QClipboard *clipboard = QApplication::clipboard();
            QString tmp = clipboard->text(QClipboard::Clipboard);
            if (tmp.length() > 0)
            {
                this->insertedChars += tmp.length();
                this->command.insert(this->command.length() - this->cursorPosition,tmp);
                this->insertPlainText(tmp);
            }
        }
        else if (e->key() == Qt::Key_C)
        {
            QClipboard *clipboard = QApplication::clipboard();
            QString tmp = this->textCursor().selection().toPlainText();

            clipboard->setText(tmp, QClipboard::Clipboard);
        }

        return;
    }

    this->cursor.movePosition(QTextCursor::End);
    this->cursor.movePosition(QTextCursor::Left, QTextCursor::MoveAnchor, this->cursorPosition);
    this->setTextCursor(this->cursor);

    if (e->key() == Qt::Key_Return)
    {
        this->cursor.movePosition(QTextCursor::End);
        this->setTextCursor(this->cursor);
        this->cursorPosition = 0;
        this->insertedChars = 0;
        emit signalCommand(command);
        this->commandHistory.append(command);
        if (this->commandHistory.size() > 10) {
            this->commandHistory.removeFirst();
        }
        this->command.clear();
        this->commandHistoryPosition = -1;
    }
    else if (e->key() == Qt::Key_Up)
    {
        if (this->commandHistory.length() -1 > this->commandHistoryPosition)
        {
            if (command.length() > 0)
            {
                this->cursor.movePosition(QTextCursor::End);
                for (int i = 0 ; i < this->insertedChars ; i++)
                    this->cursor.deletePreviousChar();
                this->cursorPosition = 0;
                this->insertedChars = 0;
                command.clear();
            }
            this->commandHistoryPosition++;
            this->command = this->commandHistory.at(this->commandHistoryPosition);
            this->insertedChars = this->command.length();
            this->insertPlainText(this->command);
        }
    }
    else if (e->key() == Qt::Key_Down)
    {
        if (this->commandHistoryPosition > 0)
        {
            if (command.length() > 0)
            {
                this->cursor.movePosition(QTextCursor::End);
                for (int i = 0 ; i < this->insertedChars ; i++)
                    this->cursor.deletePreviousChar();
                this->cursorPosition = 0;
                this->insertedChars = 0;
                command.clear();
            }
            this->commandHistoryPosition--;
            this->command = this->commandHistory.at(this->commandHistoryPosition);
            this->insertedChars = this->command.length();
            this->insertPlainText(this->command);
        }
    }
    else if (e->key() == Qt::Key_Left)
    {
        if (this->cursorPosition < this->insertedChars)
        {
            this->cursor.movePosition(QTextCursor::Left);
            this->setTextCursor(this->cursor);
            this->cursorPosition++;
        }
    }
    else if (e->key() == Qt::Key_Right)
    {
        if (this->cursorPosition > 0)
        {
            this->cursor.movePosition(QTextCursor::Right);
            this->setTextCursor(this->cursor);
            this->cursorPosition--;
        }
    }
    else if (e->key() == Qt::Key_Delete)
    {
        if (this->cursorPosition > 0)
        {
            this->cursor.movePosition(QTextCursor::Right);
            this->setTextCursor(this->cursor);
            this->cursor.deletePreviousChar();
            this->command.remove(this->command.length() - this->cursorPosition - 1, 1);
            this->insertedChars--;
            this->cursorPosition--;
        }
    }
    else if (e->key() == Qt::Key_Backspace)
    {
        if (this->insertedChars > this->cursorPosition)
        {
            this->cursor.deletePreviousChar();
            this->command.remove(this->command.length() - this->cursorPosition - 1, 1);
            this->insertedChars--;
        }
    }
    else if(e->key() == Qt::Key_Escape)
    {

    }
    else if (e->text().length()> 0)
    {
        this->insertPlainText(e->text());
        this->insertedChars++;
        this->command.insert(this->command.length() - this->cursorPosition, e->text());
    }
}

